#include "USART.h"
#include "stdio.h"
#include "GPIO_Drv.h"

int USART_PRINTF_FLAG = 2;

#if 1
#pragma import(__use_no_semihosting)  
               
struct __FILE 
{ 
	int handle; 

}; 

FILE __stdout;       
   
void _sys_exit(int x) 
{ 
	x = x; 
} 

int fputc(int ch, FILE *f)
{
	if (USART_PRINTF_FLAG == 2)
	{
		while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET);
		USART_SendData(USART2,(uint8_t)ch);
	}
	else if(USART_PRINTF_FLAG == 1)
	{
		while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);
		USART_SendData(USART1,(uint8_t)ch);
	}
	else if(USART_PRINTF_FLAG == 3)
	{
		while(USART_GetFlagStatus(USART3,USART_FLAG_TC)==RESET)
		USART_SendData(USART3,(uint8_t)ch);
	}
	
	return ch;
}
#endif

void USART1_PrintfInit(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
	
	USART1_GPIOInit();
	USART_InitTypeDef USART_InitStruct_1;
	NVIC_InitTypeDef  NVIC_InitStruct_USART1;
	
	USART_InitStruct_1.USART_BaudRate =115200 - 1;
	USART_InitStruct_1.USART_HardwareFlowControl =USART_HardwareFlowControl_None;
	USART_InitStruct_1.USART_Mode =USART_Mode_Rx|USART_Mode_Tx;
	USART_InitStruct_1.USART_Parity =USART_Parity_No;
	USART_InitStruct_1.USART_StopBits =USART_StopBits_1;
	USART_InitStruct_1.USART_WordLength =USART_WordLength_8b;
	USART_Init(USART1,&USART_InitStruct_1);
	
	USART_ClearFlag(USART1,USART_FLAG_TC);
	
	USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
	USART_ITConfig(USART1,USART_IT_IDLE,ENABLE);
	
	NVIC_InitStruct_USART1.NVIC_IRQChannel =USART1_IRQn;
	NVIC_InitStruct_USART1.NVIC_IRQChannelCmd =ENABLE;
	NVIC_InitStruct_USART1.NVIC_IRQChannelPreemptionPriority =9;//9
	NVIC_InitStruct_USART1.NVIC_IRQChannelSubPriority =0;
	NVIC_Init(&NVIC_InitStruct_USART1);
	
	USART_Cmd(USART1,ENABLE);

}

void USART2_HC05Init(void)
{
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);

	USART2_GPIOInit();
	USART_InitTypeDef USART_InitStruct_2;
	NVIC_InitTypeDef  NVIC_InitStruct_USART2;
	
	USART_InitStruct_2.USART_BaudRate =9600 - 1;
	USART_InitStruct_2.USART_HardwareFlowControl =USART_HardwareFlowControl_None;
	USART_InitStruct_2.USART_Mode =USART_Mode_Rx|USART_Mode_Tx;
	USART_InitStruct_2.USART_Parity =USART_Parity_No;
	USART_InitStruct_2.USART_StopBits =USART_StopBits_1;
	USART_InitStruct_2.USART_WordLength =USART_WordLength_8b;
	USART_Init(USART2,&USART_InitStruct_2);
	
	USART_ClearFlag(USART2,USART_FLAG_TC);
	
	USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
	USART_ITConfig(USART2,USART_IT_IDLE,ENABLE);
	
	NVIC_InitStruct_USART2.NVIC_IRQChannel =USART2_IRQn;
	NVIC_InitStruct_USART2.NVIC_IRQChannelCmd =ENABLE;
	NVIC_InitStruct_USART2.NVIC_IRQChannelPreemptionPriority =8;//8
	NVIC_InitStruct_USART2.NVIC_IRQChannelSubPriority =0;
	NVIC_Init(&NVIC_InitStruct_USART2);
	
	USART_Cmd(USART2,ENABLE);
}

/*
void Serial2_SendByte(uint8_t Byte)
{
	USART_SendData(USART2, Byte);
	while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
}

void Serial2_SendArray(uint8_t *Array, uint16_t Length)
{
	uint16_t i;
	for (i = 0; i < Length; i ++)
	{
		Serial2_SendByte(Array[i]);
	}
}

void Serial2_SendString(char *String)
{
	uint8_t i;
	for (i = 0; String[i] != '\0'; i ++)
	{
		Serial2_SendByte(String[i]);
	}
}

uint32_t Serial2_Pow(uint32_t X, uint32_t Y)
{
	uint32_t Result = 1;
	while (Y --)
	{
		Result *= X;
	}
	return Result;
}

void Serial2_SendNumber(uint32_t Number, uint8_t Length)
{
	uint8_t i;
	for (i = 0; i < Length; i ++)
	{
		Serial2_SendByte(Number / Serial2_Pow(10, Length - i - 1) % 10 + '0');
	}
}
*/
